#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
typedef pair<int, int> PII;
#define x first
#define y second
const int N = 3e5 + 10, M = 3e5 * 2 + 10;
int h[N], e[M], ne[M], idx;
int f[N];
int n1, n2, m;
int st[N];

void add(int a, int b)
{
	e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}

int bfs(int u)
{
	queue<int>q;
	q.push(u);
	int ans = 0;
	st[u] = 1;
	while (q.size()) {
		int t = q.front();
		q.pop();
		for (int i = h[t]; ~i; i = ne[i]) {
			int j = e[i];
			if (!st[j]) {
				f[j] = f[t] + 1;
				//cout << "j==" << j << endl;
				//cout << "f[j]==" << f[j] << endl;
				ans = max(ans, f[j]);
				st[j] = 1;
				q.push(j);
			}
		}
	}
	return ans;
}
int main()
{
	cin >> n1 >> n2 >> m;
	memset(h, -1, sizeof(h));
	while (m--) {
		int a, b;
		scanf("%d%d", &a, &b);
		add(a, b);
		add(b, a);
	}
	int ans1 = bfs(1);
	int ans2 = bfs(n1 + n2);
	//cout << "ans1==" << ans1 << endl;
	//cout << "ans2==" << ans2 << endl;
	cout << ans1 + ans2 + 1 << endl;
	return 0;
}